#!/bin/bash
fun_push_to_remote(){
	branch=`git rev-parse --abbrev-ref HEAD`
	remote_repo=(`git remote show`)
	count=${#remote_repo[@]}

	if [[ $count == 1 ]]; then 
		echo "git push ${remote_repo[0]} $branch"
		git push ${remote_repo[0]} $branch
	elif [[ $count > 1 ]]; then
		echo "Find more than one remote repo, select one repo to push, or all?"
		for (( i=0; i < $count; i++ ));
		do
			echo "$i) ${remote_repo[i]}"
		done
		echo "$count) all"
		read -p "Input number to select:" input < /dev/tty
		if (( $input == $count )); then
			for repo in ${remote_repo[@]}
			do 
				echo "git push $repo $branch"
				git push $repo $branch
			done
		elif (( $input >= 0 && $input < $count )); then
			echo "git push ${remote_repo[input]} $branch"
			git push ${remote_repo[input]} $branch
		else
			echo "Select nothing, cannel push."
		fi
	else
		echo "Not find remote repo."
	fi
	exit 0
}

echo "Commit success, would you want to push commit to remote？[Y/N(default)]"
for count in {10..0}
do 
	printf "\rWaiting %2ds for you..." $count
	# git hook 规定不使用标准输入流, 因此需要从终端读取
	read -t 1 input < /dev/tty
	if [ -n "$input" ]; then
		break
	fi
done

if [ "$input" == "Y" ] || [ "$input" == "y" ]
then
	fun_push_to_remote
fi
echo "Nothing to do."
